<template>
  <el-dialog :title="title"
             append-to-body
             visible
             lock-scroll
             :before-close="handleClose"
             width="700px">
    <el-form ref="form"
             :model="form"
             :label-width="ui.formLabelWidth"
             :element-loading-text="ui.loadingText"
             v-loading="ui.busying">
      <el-row type="flex"
              v-if="form.rstype !== 'GOMEZ'">
        <el-col :span="12">
          <mk-form-item label="资源名称"
                        prop="rsname"
                        :mk-le-length="100"
                        required>
            <el-input v-model="form.rsname"></el-input>
          </mk-form-item>
        </el-col>

      </el-row>

      <el-row type="flex"
              v-if="form.rstype === 'IMAGE' || form.rstype === 'VIDEO' || form.rstype === 'VOICE'">
        <el-col :span="12">
          <mk-form-item label="资源"
                        prop="rsuri"
                        mk-trigger="change"
                        required>
            <mk-upload v-model="form.rsuri"
                       :limit="1"
                       :width="210"
                       :height="30"
                       :type="form.rstype"
                       :choose="false"
                       :save-res="false"
                       list-type="text"></mk-upload>
          </mk-form-item>
        </el-col>
      </el-row>

      <el-row type="flex"
              v-else-if="form.rstype === 'EFECT'">
        <el-col :span="24">
          <mk-form-item label="动画素材"
                        prop="rsuri2"
                        mk-trigger="change"
                        required>
            <mk-upload v-model="form.rsuri2"
                       :limit="5"
                       :width="50"
                       multiple
                       :height="50"
                       type="IMAGE"
                       :choose="true"
                       list-type="text"></mk-upload>
          </mk-form-item>
        </el-col>
      </el-row>

      <el-row type="flex"
              v-else-if="form.rstype === 'GOMEZ'">
        <el-col :span="24">
          <mk-form-item label="文字素材"
                        prop="rsuri"
                        mk-trigger="change"
                        required>
            <el-input v-model="form.rsuri"></el-input>
          </mk-form-item>
        </el-col>
      </el-row>

      <el-row type="flex">
        <el-col :span="12">
          <mk-form-item prop="isactive">
            <el-switch v-model="form.isactive"
                       active-text="可见"
                       inactive-text="不可见"
                       active-value="1"
                       inactive-value="0">
            </el-switch>
          </mk-form-item>
        </el-col>
      </el-row>

      <el-row type="flex"
              v-if="form.rstype === 'IMAGE' || form.rstype === 'VIDEO' || form.rstype === 'VOICE'">
        <el-col :span="12">
          <mk-form-item label="文件夹"
                        prop="cfid"
                        :mk-le-length="32">
            <el-select v-model="form.cfid"
                       placeholder="请选择">
              <el-option v-for="item in folders"
                         :key="item.cfid"
                         :label="item.cfname"
                         :value="item.cfid">
              </el-option>
            </el-select>
          </mk-form-item>
        </el-col>
      </el-row>

      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="备注"
                        prop="remark"
                        :mk-le-length="500">
            <el-input v-model="form.remark"></el-input>
          </mk-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer">
      <mk-button @click="handleClose">取消</mk-button>
      <mk-button @click="onSubmit"
                 :type="2">{{btnName}}</mk-button>
    </div>
  </el-dialog>
</template>

<script>
import { MkBase } from 'element-ui'
import formData from './form-data'
import MkUpload from 'sbin/upload'
export default {
  name: 'cm-resource-form',
  mixins: [MkBase],
  props: {
    isAdd: Boolean,
    title: String,
    rstype: String,
    btnName: String,
    row: Object
  },
  async mounted () {
    if (this.isAdd === false) {
      this.form = await this.$get('cmResource/view.json', { id: this.row.rsid })
      if (this.rstype === 'EFECT' && this.form.rsuri) {
        this.form.rsuri2 = this.form.rsuri.split(',')
      }
    } else {
      this.form.isactive = '1'
      this.form.rstype = this.rstype
      this.form.cfid = this.row.rsid
    }
    await this.loadFolder()
    this.ui.busying = false
  },
  data () {
    const form = formData()
    return {
      ui: {
        formLabelWidth: '100px',
        busying: true,
        loadingText: '正在加载'
      },
      form,
      folders: []
    }
  },
  methods: {
    async loadFolder () {
      let data = await this.$get('cmResourceFolder/getListByRstype.json', {
        rstype: this.rstype
      })
      this.folders = data
    },
    async valid () {
      if (!this.validate) {
        let fn = this.$refs.form.validate
        let nodejs = false
        this.validate = this.$promise({ fn, nodejs })
      }
      return await this.validate()
    },
    submitUpload () {
      this.$refs.upload.submit()
    },
    async onSubmit () {
      if (await this.valid()) {
        this.ui.loadingText = '正在保存'
        this.ui.busying = true
        try {
          let data = await this[this.isAdd === false ? '$put' : '$post'](
            `/cmResource/${this.isAdd === false ? 'update' : 'add'}.json`,
            {
              cmResource: this.form,
              rsuris: this.form.rsuri2 && this.form.rsuri2.join()
            }
          )
          if (data) {
            this.$emit('close', true)
          }
        } finally {
          this.ui.busying = false
        }
      }
    },
    handleAvatarSuccess (data, param, p2, p3) {
      this.form.rsuri = data.url
    },
    handleClose () {
      this.$emit('close')
    }
  },
  components: {
    MkUpload
  }
}
</script>
